package com.iwdnb.gkgz.application.utils;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import com.iwdnb.bmnf.common.thread.TtlThreadPoolTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

public class ExecutorUtils {
    private static final Executor executor= initexecutor();

    private static Executor initexecutor() {
        ThreadPoolTaskExecutor executor = new TtlThreadPoolTaskExecutor();
        //核心线程池大小
        executor.setCorePoolSize(10);
        //最大线程数
        executor.setMaxPoolSize(20);
        //队列容量
        executor.setQueueCapacity(10000);
        //活跃时间
        executor.setKeepAliveSeconds(30);
        //线程名字前缀
        executor.setThreadNamePrefix("stockBackExecutor-");

        // setRejectedExecutionHandler：当pool已经达到max size的时候，如何处理新任务
        // CallerRunsPolicy：不在新线程中执行任务，而是由调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }

    public static Executor getExecutor(){
        return executor;
    }
}
